package xian.woniuxy.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xian.woniuxy.entity.GoodsType;
import xian.woniuxy.service.IGoodsTypeService;
import xian.woniuxy.util.ResultVO;

import java.util.List;

/**
 * @author ljy
 * @time 2021/12/25 13:23:04
 */
@RequestMapping("goodstype")
@RestController
public class GoodsTypeController {
    @Autowired
    private IGoodsTypeService goodsTypeService;
    ResultVO resultVO=new ResultVO();
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable int id){
        goodsTypeService.delete(id);
        return new ResultVO(200, "delete ok", null);
    }
    @PutMapping
    public ResultVO update(@RequestBody GoodsType goodsType) {
        goodsTypeService.update(goodsType);
        return new ResultVO(200, "update ok", null);
    }
    @PostMapping
    public ResultVO save(@RequestBody GoodsType goodsType){
        goodsTypeService.save(goodsType);
        return new ResultVO(200, "save ok", null);
    }
    @GetMapping
    public  ResultVO findAll(){
        try {

            List<GoodsType> list =goodsTypeService.getAll();
            resultVO.setCode(200);
            resultVO.setMsg("查询成功");
            resultVO.setData(list);
        }catch (Exception e){
            resultVO.setCode(500);
            resultVO.setMsg("查询失败");
        }
        return  resultVO;
    }


    @GetMapping("/{type_id}")
    public  ResultVO getOne(@PathVariable int type_id){
        try {

            List<GoodsType> goodsType =goodsTypeService.getOne(type_id);
            resultVO.setCode(200);
            resultVO.setMsg("查询成功");
            resultVO.setData(goodsType);
        }catch (Exception e){
            resultVO.setCode(500);
            resultVO.setMsg("查询失败");
            System.out.println(e.getMessage());
        }
        return  resultVO;
    }
}
